package com.moorror.codegenvel.common.tree.actions.gencode;

import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.fileChooser.FileChooser;
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.moorror.codegenvel.utils.ProjectUtils;
import com.moorror.codegenvel.view.window.codegen.domain.ExtractFileDesc;
import org.jetbrains.annotations.NotNull;

import java.util.function.Consumer;

/**
 * @Author jiyanghuang
 * @Date 2025/8/24 17:38
 */
public class ExtractVelocityFileChooseAction extends AnAction implements DumbAware {

    private final ExtractFileDesc extractFileDesc;
    private final Consumer<ExtractFileDesc> consumer;

    public ExtractVelocityFileChooseAction(ExtractFileDesc extractFileDesc, Consumer<ExtractFileDesc> consumer) {
        super("File Chooser", "File Chooser", AllIcons.Actions.MenuOpen);
        this.extractFileDesc = extractFileDesc;
        this.consumer = consumer;
    }

    @Override
    public void actionPerformed(@NotNull AnActionEvent e) {

        Project project = e.getProject();
        //将当前选中的model设置为基础路径
        VirtualFile path = ProjectUtils.fetchBaseDir(project);
        VirtualFile virtualFile = FileChooser.chooseFile(FileChooserDescriptorFactory.createSingleFileDescriptor(), project, path);
        if (virtualFile != null) {
            extractFileDesc.fileChooser(project, virtualFile);
            if(null != consumer){
                consumer.accept(extractFileDesc);
            }
        }
    }
}
